package top.anonyeast.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import top.anonyeast.pojo.IPAddress;
import top.anonyeast.service.IPAddressService;
import top.anonyeast.util.IpUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class IPAddressController {
    @Resource(name = "ipUtils")
    private IpUtils ipUtils;
    @Resource(name = "IPAddressService")
    private IPAddressService service;

    /**
     * 插入并返回最近访问的ip地址列表
     * @param request
     * @param amount
     * @return
     */
    @PostMapping("/insertAndGetIpInfo")
    public List<IPAddress> insertAndGetIpInfo(HttpServletRequest request,int amount){
        String outerIp = ipUtils.getOuterIp(request);
        return service.insertAndGetIpInfo(outerIp,amount);
    }

    /**
     * 插入登录失败的IP
     * @param request
     * @param msg
     * @return IP地址对象
     */
    public IPAddress insertLoginFailedIpInfo(HttpServletRequest request,String msg){
        String outerIp = ipUtils.getOuterIp(request);
        return service.insertLoginFailedIpInfo(outerIp,msg);
    }
}
